package cn.xiaohuodui.service;

import cn.xiaohuodui.constant.ErrorCodeDefine;
import cn.xiaohuodui.dao.CategoryAttributeMapper;
import cn.xiaohuodui.dao.GoodCategoryAttributeMapper;
import cn.xiaohuodui.exceptions.BussException;
import cn.xiaohuodui.model.Category;
import cn.xiaohuodui.model.CategoryAttribute;
import cn.xiaohuodui.model.Good;
import cn.xiaohuodui.model.GoodCategoryAttribute;
import cn.xiaohuodui.vo.GoodCategoryAttribute.GoodCateAttVo;
import cn.xiaohuodui.vo.index.CateRecGoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by TR on 2018/4/3.
 */
@Service
public class GoodCateAttService {

    @Autowired
    GoodCategoryAttributeMapper goodCategoryAttributeMapper;

    @Autowired
    CategoryAttributeMapper categoryAttributeMapper;

    public List<GoodCateAttVo>  getByGoodId(Integer id) throws BussException {
        List<GoodCategoryAttribute> byGoodId = goodCategoryAttributeMapper.getByGoodId(id);
        List<GoodCateAttVo> goodCateAttVos = new ArrayList<>();
        for(GoodCategoryAttribute goodCategoryAttribute:byGoodId){
            GoodCateAttVo goodCateAttVo = AssembleGoodCateAttVo(goodCategoryAttribute);
            goodCateAttVos.add(goodCateAttVo);
        }
        return goodCateAttVos;
    }

    //装配方法
    public GoodCateAttVo AssembleGoodCateAttVo(GoodCategoryAttribute goodCategoryAttribute ) throws BussException {
        CategoryAttribute categoryAttribute = categoryAttributeMapper.selectByPrimaryKey(goodCategoryAttribute.getAttributeId());
        if(categoryAttribute==null){
            throw new BussException(ErrorCodeDefine.CATEGORY_ATTRIBUTE_NOT_EXIST.getCode(),ErrorCodeDefine.CATEGORY_ATTRIBUTE_NOT_EXIST.getDesc());
        }
        GoodCateAttVo goodCateAttVo = new GoodCateAttVo();
        goodCateAttVo.setAttributeId(goodCategoryAttribute.getAttributeId());
        goodCateAttVo.setName(categoryAttribute.getName());
        goodCateAttVo.setValue(goodCategoryAttribute.getValue());
        return goodCateAttVo;
    }
}
